/*
============================================================================
 Name        : StreamReader.h
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : CStreamReader declaration
============================================================================
*/

#ifndef __STREAMREADER_H__
#define __STREAMREADER_H__

// INCLUDES
#include <e32std.h>
#include <e32base.h>

#include "Connection.h"

#include "logger.h"

class CStreamBuffer;

class CStreamReader : public MConnectionObserver
{
public: // Constructors and destructor
	IMPORT_C static CStreamReader* NewL();
	IMPORT_C static CStreamReader* NewLC();
	IMPORT_C ~CStreamReader();

protected:
	IMPORT_C CStreamReader();
	IMPORT_C virtual void ConstructL();


private: //from MConnectionObserver
	IMPORT_C void ResponseRequestedL(TDesC8& /*aResponse*/) {};
	IMPORT_C void StreamingRequestedL(TDesC8& aChunk);
	IMPORT_C void StreamingEndedL();
	IMPORT_C void ResponseErrorL(TInt aError);


public: //new public functions
	IMPORT_C void SetUrl(const TDesC& aUrl);
	IMPORT_C void StartL();
	void WriteToBuffer();


protected: //virtual functions
	IMPORT_C virtual const TDesC8& GetAdditionalHeadersL();
	IMPORT_C virtual TDesC8& ParseResponseL(TDesC8& aChunk);


protected: //new protected functions
	IMPORT_C void FinishedL();


private: //new functions
	void SetStreamBuffer(CStreamBuffer* aStreamBuffer);
	void ReceiveL();
	void WriteToBuffer(TDesC8& aChunk);

	friend class CStreamPlayer;

protected: //data
	//Connection object
	CConnection* iConnection;
	
	//Url
	HBufC *iUrlRequest;

	//Stream buffer
	CStreamBuffer* iStreamBuffer;

	//Reference to last data chunk read
	TDesC8* iLastChunk;

	//Check if is pending to read
	TBool iIsPendingToRead;
};

#endif // __STREAMREADER_H__
